package hazesoft.Collection;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences.Editor;

public class CollectionSettingsActivity extends Activity {
	
	public void MessageBox(String message){
	    Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
	}
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.settings);
        
      
        Button btnRemove = (Button)findViewById(R.id.btnRemove);
        btnRemove.setOnClickListener(btnRemove_OnClick);

        try
        {
        
        Button btnSetup = (Button)findViewById(R.id.btnSetup);
        btnSetup.setOnClickListener(btnSetup_OnClick);
       
         
        if(GetPref("FIRST") == "false")
        	btnSetup.setEnabled(false);
        	
        
        EditText edtAddress = (EditText)findViewById(R.id.edtAddress);
	    EditText edtPID = (EditText)findViewById(R.id.edtPID);
		
		edtAddress.setText(GetPref("SERVER_ADDRESS"));
		edtPID.setText(GetPref("PHONE_ID"));
		
		if(GetPref("SERVER_ADDRESS") == "")
		{
			SavePref("SERVER_ADDRESS", "http://www.africanhoneybee.net/Upload.aspx");
		}
        }catch(Exception e)
        {
        	MessageBox(e.toString());
        	e.printStackTrace();
        }
		
        
    }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.settings_menu, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.menuSave:
	    	EditText edtAddress = (EditText)findViewById(R.id.edtAddress);
			EditText edtPID = (EditText)findViewById(R.id.edtPID);
			
			SavePref("SERVER_ADDRESS", edtAddress.getText().toString());
			SavePref("PHONE_ID", edtPID.getText().toString());
			CollectionSettingsActivity.this.finish();
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	};
	
	public void SavePref(String Key, String Value)
	{
		Editor e = this.getSharedPreferences("AHB",MODE_PRIVATE).edit();
		e.putString(Key, Value);
		e.commit();
		MessageBox("Saving Pref");
		
	};
	
	public String GetPref(String Key)
	{
		return this.getSharedPreferences("AHB",MODE_PRIVATE).getString(Key, null);
	};
	
	public View.OnClickListener btnSave_OnClick = new View.OnClickListener() {
		
		public void onClick(View arg0) {
		
			EditText edtAddress = (EditText)findViewById(R.id.edtAddress);
			EditText edtPID = (EditText)findViewById(R.id.edtPID);
			
			SavePref("SERVER_ADDRESS", edtAddress.getText().toString());
			SavePref("PHONE_ID", edtPID.getText().toString());
			CollectionSettingsActivity.this.finish();
			
			
		}
	};
	
	public View.OnClickListener btnSetup_OnClick = new View.OnClickListener() {
		
		public void onClick(View v) {
			Intent i = new Intent(CollectionSettingsActivity.this, Collection_Setup.class);
			 startActivity(i);
			
		}
	};
	
public View.OnClickListener btnRemove_OnClick = new View.OnClickListener() {
		
		public void onClick(View arg0) {
			String[] Files = fileList();
			for(int x = 0; x < Files.length; x++)
			{
				deleteFile(Files[x]);
			}
		}
	};


}
